#!/usr/bin/env python
from db.model import Model

#绘制用户服务界面
def welcomeUI():
    print("*"*36)
    print("")
    print("{:<5}{:<10}{:>10}{:>5}".format(" ","1. 存钱","2. 查询余额"," "))
    print("{:<5}{:<10}{:>10}{:>5}".format(" ","3. 取钱","4. 退出登录"," "))
    print("")
    print("*"*36)

#实现用户登录功能
def userLogin():
    userName = input("欢迎使用自助结算系统,请输入您的用户名:")
    if userName in userInfo:
        userPassword = input("欢迎您"+userName+"，请输入您的密码:")
        if userPassword == userInfo[userName]:
            print("欢迎您"+ userName +"，很高兴为您服务！")
            welcomeUI()
            return 1,userName
        else:
            print("您输入的密码有误，请重新登录")
    else:
        print("您输入的用户名不存在，请重新登录")
    return 0,userName

#实现用户余额查询功能
def checkBalance(mod,userName):
    if userName in userInfo:
        userMoneyLast = mod.find(userName)
        print("您的余额是：{}".format(userMoneyLast['deposit']))
    else:
        print("您的用户信息存在问题，请联系银行管理人员！")

#实现用户存款功能
def saveMoney(mod,userName):
    if userName in userInfo:
        deposit = input("欢迎您，"+userName+"请输入您的存入金额:")
        while (deposit.strip() == ''or (not deposit.isdigit()) or int(deposit) <= 0):
            deposit = input("您的输入金额有误，请重新输入您的存入金额:")
        else:
            deposit = int(deposit)

        userMoneyLast = mod.find(userName)
        userMoneyLast['deposit'] += deposit
        data = [userName,userMoneyLast['deposit']]
        mod.update(data)
    else:
        print("您的用户信息存在问题，请联系银行管理人员！")

#实现用户取款功能
def drawOutMoney(mod,userName):
    if userName in userInfo:
        withdrawal = input("欢迎您，"+userName+"请输入您的取款金额:")
        testCount = 3
        userMoneyLast = mod.find(userName)
        while (withdrawal.strip() == '' or (not withdrawal.isdigit()) or int(withdrawal) <= 0 or (int(withdrawal) > userMoneyLast['deposit'] and testCount > 0)):
            withdrawal = input("您的输入金额有误或没有足够的余额可提取，请重新输入您的取款金额:")
            testCount -= 1
        else:
            withdrawal = int(withdrawal)

        if withdrawal < userMoneyLast['deposit']:
            userMoneyLast['deposit'] -= withdrawal
        data = [userName, userMoneyLast['deposit']]
        mod.update(data)
    else:
        print("您的用户信息存在问题，请联系银行管理人员！")

#程序主入口
if __name__ == '__main__':
    # 初始化用户信息，字典存储“用户名：密码”
    userInfo = {}
    # 初始化用户存款账号，字典存储“用户名：余额”
    userMoney = {}
    mod = Model("users")
    alist = mod.findAll()
    userInfo = {}
    # userMoney = {}
    for index in range(len(alist)):
        userInfo[alist[index]['username']] = alist[index]['passwd']
        # userMoney[alist[index]['username']] = alist[index]['deposit']
    flag = 1
    while(flag):
        loginStatus = userLogin()
        if loginStatus[0]:
            while(flag):
                selectService = input("请选择您需要的服务编号:")
                while(selectService not in ["1","2","3","4"]):
                    selectService = input("您选择的服务编号有误，请重新输入您需要的服务编号:")
                if "1" == selectService:
                    saveMoney(mod, loginStatus[1])
                    continue
                elif "2" == selectService:
                    checkBalance(mod, loginStatus[1])
                    continue
                elif "3" == selectService:
                    drawOutMoney(mod, loginStatus[1])
                    continue
                else:
                    flag = 0
                    break

